package doudou.util;

import doudou.pojo.*;
import java.io.*;
import java.util.*;

//把系统中关键性的方法，统一编写
public class CardUtil {
    static Map<String, MobileCard> cards = new HashMap<>();
    static Map<String, List<ConsumInfo>> consumInfos = new HashMap<>();
    static MobileCard mc[];
    static MobileCard userLogin;
    static int count = 9;
    static Scanner sc = new Scanner(System.in);
    static String numbers[] = new String[count];
    static FileWriter fw;
    static  boolean isRunning=true;

    static {
        try {
            fw = new FileWriter("C:\\Users\\梅穿内裤\\Desktop\\doudou\\资费说明.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
        input();
        newinput();
        mc = cards.values().toArray(new MobileCard[0]);
    }
    public static void initScene(){
        while (true){
            System.out.println();
            System.out.println("*******欢迎使用兜兜5G移动营业大厅*******");
            System.out.println("1.用户登录\t2.用户注册\t3.使用兜兜5G\t\t4.话费充值\t5.咨费说明\t6.退出系统");
            System.out.print("请输入对应的编号:");
            int n= sc();
            switch (n){
                case 1:loginUser();break;
                case 2:addCard();break;
                case 3:useDoudou();break;
                case 4:chargeMoney();break;
                case 5:showDescription();break;
                case 6:return;
                default:
                    System.out.println("输入错误，请重新输入!");
                    break;
            }
        }
    }

    public static void loginUser(){
        mc = cards.values().toArray(new MobileCard[0]);
        System.out.println("执行用户登录功能");
        System.out.print("请输入电话号码:");
        String phone=sc.next();
        System.out.print("请输入密码:");
        String password=sc.next();
        boolean b=isExistCard(phone,password);
        if (b){
            System.out.println("***尊敬的用户"+phone+"欢迎您***");
            userLogin=cards.get(phone);
            userMenu();
        }else {
            System.out.println("用户名或者密码错误!");
        }
    }
    public static boolean isExistCard(String number, String passWord) { //登录时判断号码是否已经存在
        for (MobileCard m:mc){
            if (m.getCardNumber().equals(number)&&m.getPassWord().equals(passWord)){
                return true;
            }
        }
        return false;
    }
    public static boolean isExistCard(String number) { // 注册判断号码是否已经存在
        for (MobileCard m : mc) {
            if (m.getCardNumber().equals(number)) {
                return true;
            }
        }
        return false;
    }

    public static String createNumber(){ //创建号码，随机数
        Random rd=new Random();
        String phone="139";
        for (int i=0;i<8;i++){
            int num=rd.nextInt(10);
            phone+=+num;
        }
        return phone;
    }

    public static String[] getNewNumbers(int count){  //创建的号码保存在集合中(9个)
        String[] newNumbers = new String[count];
        for (int i=0;i<count;i++) {
            String phone;
            phone=createNumber();
            if (!isExistCard(phone)){
               newNumbers[i]=phone;
               numbers[i]=phone;
            }else {
               i--;
            }
        }
        return newNumbers;
    }

    public static void userMenu(){
        while (isRunning){
            System.out.println("1.本月账单查询");
            System.out.println("2.套餐余额查询");
            System.out.println("3.打印消费详单");
            System.out.println("4.套餐变更");
            System.out.println("5.办理退网");
            System.out.print("请选择(输入1~5选择功能，其他键返回上一级):");
            int n=sc();
            switch (n){
                case 1:showAmountDetail();break;
                case 2:showRemainDetail();break;
                case 3:printConsumInfo();break;
                case 4:changingPack();break;
                case 5:delCard();break;
                default:return;
            }
        }
    }
    public static void addCard() { // 用户注册
        System.out.println("执行用户注册功能!");
        String numbers[] = getNewNumbers(count);
        String phoneNumber = "";
        for (int i = 1; i <= count; i++) {
            System.out.print(i + "." + numbers[i - 1] + "\t");
            if (i % 3 == 0) {
                System.out.println();
            }
        }
        boolean bl=true;
        while (bl){
            System.out.print("请选择卡号:");
            int num = sc();
            if (num>0&&num<=9){
                phoneNumber = numbers[num - 1];
                bl=false;
            }else {
                System.out.println("输入错误，请输入正确的编号!");
            }
        }

        System.out.println("套餐选择: 1.话痨套餐，2.网虫套餐，3.超人套餐");
        System.out.print("请选择套餐(输入序号):");
        int n = sc();
        ServicePackage serPackage = null;
        switch (n) {
            case 1:
                serPackage = new TalkPackage();
                break;
            case 2:
                serPackage = new NetPackage();
                break;
            case 3:
                serPackage = new SuperPackage();
                break;
            default:
                System.out.println("套餐选择错误！");
                return;
        }
        System.out.print("请输入姓名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        boolean boo=true;
        double money=0;
        while (boo){
            System.out.print("请输入预存话费金额:");
             money= sc2();
            if (money >= serPackage.getPrice()) {
                money = money - serPackage.getPrice();
                boo=false;
            } else {
                System.out.print("您预存的话费不足以支付本月固定套餐资费，请重新充值:");
            }
        }

        MobileCard newCard = new MobileCard(phoneNumber, username, password, serPackage, serPackage.getPrice(), money, serPackage.getTalkTime(), serPackage.getSmsCount(), serPackage.getFlow());
        cards.put(phoneNumber, newCard);
        mc = cards.values().toArray(new MobileCard[0]);
        output();
        System.out.println(username + "注册成功!");
        System.out.println("卡号:" + phoneNumber + "\t用户名:" + username + "\t当前余额:" + money + "元");
        System.out.println(serPackage.getServiceName() +
                ":通话时长为:" + serPackage.getTalkTime() +
                "分钟/月,短信条数为:" + serPackage.getSmsCount() +
                "条/月,上网流量为:" + serPackage.getFlow() + "MB/月");
    }

    public static void delCard(){  //退网
        System.out.println("执行办理退网功能！");
        boolean b=isExistCard(userLogin.getCardNumber());
        if (b){
            System.out.print("您确定要注销卡号吗(属于y确定，输入其他返回):");
            String str=sc.next();
            if ("y".equals(str)){
                cards.remove(userLogin.getCardNumber());
                output();
                System.out.println("非常遗憾无法再为您服务!");
                isRunning=false;
                initScene();
            }else {
                System.out.println("您的选择很正确!");
            }
        }else {
            System.out.println("用户名或者密码错误!");
        }
    }

    public static void showRemainDetail(){ //余量查询
         System.out.println("执行套餐余额查询功能:");
         System.out.println("卡号"+userLogin.getCardNumber()+",套内余额为:");
         System.out.println("通话时长:"+userLogin.getRealTalkTime()+"分钟");
         System.out.println("短信条数:"+userLogin.getRealSMSCount()+"条");
         System.out.println("上网流量:"+userLogin.getRealFlow()+"MB");

    }

    public static void showAmountDetail(){ //账单查询
        System.out.println("执行本月账单查询功能:");
        System.out.println("卡号:"+userLogin.getCardNumber());
        System.out.println("套餐资费:"+userLogin.getSerPackage().getPrice());
        System.out.println("合计:"+userLogin.getConsumAmount());
        System.out.println("余额:"+userLogin.getMoney());
    }

    public static void addConsumInfo(String number, ConsumInfo info) {
        if (consumInfos.containsKey(number)) {
            List<ConsumInfo> infoList = consumInfos.get(number);
            infoList.add(info);
        } else {
            List<ConsumInfo> infoList = new ArrayList<>();
            infoList.add(info);
            consumInfos.put(number, infoList);
        }
        newoutput();
    }

    public static void useDoudou(){
        Random rd=new Random();
        System.out.println("执行使用兜兜5G功能");
        System.out.print("请输入电话号码:");
        String phone=sc.next();
        boolean b=isExistCard(phone);
        if (b){
            int n= rd.nextInt(6);
            switch (n){
                case 0:call1(phone);break;
                case 1:call2(phone);break;
                case 2:sendText1(phone);break;
                case 3:sendText2(phone);break;
                case 4:play1(phone);break;
                case 5:play2(phone);break;
            }
        }else {
            System.out.println("输入错误，号码不存在!");
        }

    }
    public static void call1(String phone){  //模拟通话场景
        System.out.println("问候客户，谁知其如此难缠，通话90分钟:");
        int time=90;
        MobileCard m=cards.get(phone);
        TalkPackage tp=new TalkPackage();
        int useTime=tp.call(time,m);
        ConsumInfo c=new ConsumInfo("通话",phone,useTime);
        addConsumInfo(phone,c);
        output();
        newoutput();

    }
    public static void call2(String phone){
        System.out.println("和女朋友煲电话粥，通话180分钟");
        int time=180;
        MobileCard m=cards.get(phone);
        TalkPackage tp=new TalkPackage();
        int useTime=tp.call(time,m);
        ConsumInfo c=new ConsumInfo("通话",phone,useTime);
        addConsumInfo(phone,c);
        output();
        newoutput();
    }
    public static void sendText1(String phone){
        System.out.println("换了新的手机号，通知朋友，发送短信50条");
        int textNum=50;
        MobileCard m=cards.get(phone);
        SuperPackage sp=new SuperPackage();
        int useText=sp.send(textNum,m);
        ConsumInfo c=new ConsumInfo("短信",phone,useText);
        addConsumInfo(phone,c);
        output();
        newoutput();

    }
    public static void sendText2(String phone){
        System.out.println("参与保护环境的问卷调查，发送短信5条");
        int textNum=5;
        MobileCard m=cards.get(phone);
        SuperPackage sp=new SuperPackage();
        int useText=sp.send(textNum,m);
        ConsumInfo c=new ConsumInfo("短信",phone,textNum);
        addConsumInfo(phone,c);
        output();
        newoutput();

    }
    public static void play1(String phone){
        System.out.println("网瘾少年，打了3个小时的游戏，用了1.5G流量");
        int netNum=1500;
        MobileCard m=cards.get(phone);
        NetPackage np=new NetPackage();
        int useNet=np.netPlay(netNum,m);
        ConsumInfo c=new ConsumInfo("上网",phone,useNet);
        addConsumInfo(phone,c);
        output();
        newoutput();
    }
    public static void play2(String phone){
        System.out.println("躺在床上刷了半个小时的视频，用了500M流量");
        int netNum=500;
        MobileCard m=cards.get(phone);
        NetPackage np=new NetPackage();
        int useNet=np.netPlay(netNum,m);
        ConsumInfo c=new ConsumInfo("上网",phone,useNet);
        addConsumInfo(phone,c);
        output();
        newoutput();
    }
    public static void showDescription(){ //资费说明
        ServicePackage package1=new TalkPackage();
        ServicePackage package2=new NetPackage();
        ServicePackage package3=new SuperPackage();
        package1.showInfo();
        System.out.println("________________________");
        package2.showInfo();
        System.out.println("________________________");
        package3.showInfo();
        System.out.println("________________________");
        System.out.println("超出套餐计费:");
        System.out.println("通话时长:0.2元/分钟");
        System.out.println("短信条数:0.1元/条");
        System.out.println("上网流量:0.1元/MB");
    }

    public static void changingPack(){ //更改套餐
        System.out.println("执行套餐变更功能!");
        System.out.println("您的电话号码为:"+userLogin.getCardNumber());
        System.out.println("套餐选择: 1.话痨套餐，2.网虫套餐，3.超人套餐");
        System.out.print("请选择套餐(输入序号):");
        int n = sc();
        ServicePackage serPackage ;
        switch (n) {
            case 1:
                serPackage = new TalkPackage();
                break;
            case 2:
                serPackage = new NetPackage();
                break;
            case 3:
                serPackage = new SuperPackage();
                break;
            default:
                System.out.println("套餐选择错误！");
                return;
        }
        if (userLogin.getSerPackage().getServiceName().equals(serPackage.getServiceName())){
            System.out.println("您已选择该套餐，无法更改!");
        }else if(serPackage.getPrice()>userLogin.getMoney()){
                System.out.println("您的余额不足，无法更换该套餐!");
            }else {
                userLogin.setSerPackage(serPackage);
                userLogin.setMoney(userLogin.getMoney()- serPackage.getPrice());
                userLogin.setRealTalkTime(serPackage.getTalkTime());
                userLogin.setRealSMSCount( serPackage.getSmsCount());
                userLogin.setRealFlow(serPackage.getFlow());
                userLogin.setConsumAmount(userLogin.getConsumAmount()+ serPackage.getPrice());
                cards.put(userLogin.getCardNumber(),userLogin);
                output();
                System.out.println("套餐成功更改为:"+serPackage.getServiceName());
                output();
            }
        }

    public static void printConsumInfo() {
        if (userLogin == null) {
            System.out.println("请先登录！");
            return;
        }
        String number = userLogin.getCardNumber();
        List<ConsumInfo> list = consumInfos.get(number);
        if (list == null || list.isEmpty()) {
            System.out.println("没有找到该号码的消费记录！");
            return;
        }
        System.out.println("******" + number + "消费记录******");
        System.out.println("序号\t类型\t数据(电话(分钟)/短信(条)/流量(MB)");
        for (int i=0; i<list.size();i++) {
            ConsumInfo item=list.get(i);
            System.out.print((i+1) + ".\t");
        if ("通话".equals(item.getType())) {
            System.out.print("通话\t"+item.getConsumDate()+"\n");
        } else if ("短信".equals(item.getType())) {
            System.out.print("短信\t"+item.getConsumDate()+"\n");
        } else if ("上网".equals(item.getType())) {
            System.out.print("流量\t"+item.getConsumDate()+"\n");
        }
    }
}

    public static void chargeMoney(){ //充话费
        System.out.println("执行话费充值功能!");
        System.out.print("请输入电话号码:");
        String phone= sc.next();
        System.out.print("请输入充值的金额(50元起):");
        double money=sc2();
        boolean bo=isExistCard(phone);
        if (bo){
            if (money<50){
                System.out.println("您充值的话费少于50元!");
            }else {
                boolean b=isExistCard(phone);
                if (b){
                    MobileCard m=cards.get(phone);
                    double newMoney=money+m.getMoney();
                    m.setMoney(newMoney);
                    System.out.println("充值话费"+money+"成功，您的余额为"+newMoney);
                    output();
                }
            }
        }else {
            System.out.println("电话号码不存在！");
        }
    }

    public static void main(String[] args) {
        initScene();
    }
    public static int sc(){
        int n=0;
        boolean b=false;
        while (!b){
            try {
                n=sc.nextInt();
                b=true;
            }catch (InputMismatchException e){
                System.err.println("文本输入错误！");
                sc.nextLine();
            }
        }
        return n;
    }
    public static double sc2(){
        double n=0;
        boolean b=false;
        while (!b){
            try {
                n=sc.nextDouble();
                b=true;
            }catch (InputMismatchException e){
                System.err.println("请输入正确的文本！");
                sc.nextLine();
            }
        }
        return n;
    }
    public static void input(){
        try{
            ObjectInputStream ois=new ObjectInputStream(new FileInputStream("C:\\Users\\梅穿内裤\\Desktop\\doudou\\userMassage.txt"));
            cards=(Map<String,MobileCard>)ois.readObject();
            ois.close();
        }catch (FileNotFoundException e){
            System.out.println("欢迎您第一次使用!");
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    public static void output(){
         try {
             ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("C:\\Users\\梅穿内裤\\Desktop\\doudou\\userMassage.txt"));
             oos.writeObject(cards);
             oos.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
    public static void newinput(){
        try{
            ObjectInputStream ois=new ObjectInputStream(new FileInputStream("C:\\Users\\梅穿内裤\\Desktop\\doudou\\消费清单.txt"));
            consumInfos=(Map<String, List<ConsumInfo>>) ois.readObject();
            ois.close();
        }catch (FileNotFoundException e){
            System.out.println("欢迎您第一次使用!");
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    public static void newoutput(){
        try {
            ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("C:\\Users\\梅穿内裤\\Desktop\\doudou\\消费清单.txt"));
            oos.writeObject(consumInfos);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

